/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.corespring.restaurant.dao.hibernate;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.corespring.restaurant.dao.IngredienteDao;
import org.corespring.restaurant.model.Ingrediente;
import org.corespring.restaurant.model.Unidad;

/**
 *
 * @author Aldo
 */
@Repository
public class IngredienteDaoHibernate extends HibernateDaoSupport implements IngredienteDao {

    @Autowired
    public IngredienteDaoHibernate(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    @Override
    public List<Ingrediente> obtenerIngredientes(Ingrediente oIng) {
        StringBuilder query = new StringBuilder("from Ingrediente m where 1 = 1 ");

        if (oIng.getDesing() != null
                && !oIng.getDesing().isEmpty()) {
            query.append(" and upper(m.desing) like '%").append(oIng.getDesing().toUpperCase()).append("%'");
        }

        return getHibernateTemplate().find(query.toString());
    }

    @Override
    public List<Ingrediente> obtenerIngredientes() {
        return getHibernateTemplate().find("from Ingrediente");
    }

    @Override
    public Ingrediente obtenerIngrediente(String sId) {
        Ingrediente ingrediente = getHibernateTemplate().get(Ingrediente.class, sId);
        return ingrediente;
        //return (Ingrediente) getSession().createQuery("from Ingrediente "
        //                + "where iding=?").setString(sId, sId).uniqueResult();
    }

    @Override
    public void grabarIngrediente(Ingrediente objIng) {
        getHibernateTemplate().saveOrUpdate(objIng);
        getHibernateTemplate().flush();
    }

    @Override
    public void eliminarIngrediente(String sId) {
        Ingrediente oIngrediente = obtenerIngrediente(sId);
        getHibernateTemplate().delete(oIngrediente);
    }

    @Override
    public List<Unidad> obtenerUnidades() {
        return getHibernateTemplate().find("from Unidad");
    }
    @Override
    public List<Ingrediente> obtenerListaIngredientesNoSeleccionados(String ids) {
        String sql="";
        if(ids.equals("")) sql = " from Ingrediente ";
        else sql = " from Ingrediente where iding not in (" + ids + ") ";
        List list = getHibernateTemplate().find(sql);
        return list;
    }
}
